package com.itjob.deep.util;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletResponse;

public class CookieUtil {

	private CookieUtil(){}
	
	public static Cookie getCookie(Cookie[] cookies, String key) {
		
		if (cookies == null || cookies.length == 0 || key == null) {
			return null;
		}
		
		Cookie cookie = null;
		for (Cookie c : cookies) {
			if (c.getName().equals(key)) {
				cookie = c;
				break;
			}
		}
		
		return cookie;
	}
	
	public static void setCookie(String name, String value, int maxAge, HttpServletResponse response) {
		Cookie cookie = new Cookie(name, value);
		cookie.setMaxAge(maxAge);
		
		response.addCookie(cookie);
	}
}
